package com.zhuiYun.project.api.commoditycomment.service.impl;

import com.zhuiYun.project.api.commoditycomment.entity.CommodityComment;
import com.zhuiYun.project.api.commoditycomment.service.CommodityCommentService;
import com.zhuiYun.project.api.commoditycomment.mapper.CommodityCommentMapper;
import com.zhuiYun.project.api.order.entity.Order;
import com.zhuiYun.project.api.order.mapper.OrderMapper;
import com.zhuiYun.project.common.errorCode.EmErrorCode;
import com.zhuiYun.project.common.response.CommonResult;
import com.zhuiYun.project.util.ObjectUtils;
import com.zhuiYun.project.util.UploadFileUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;

/**
 * @ClassName CommodityCommentServiceImpl
 * @Description TODO 商品评论的service层
 * @Author 赵笛
 * @Date 2023/4/19 20:57
 **/
@Service
public class CommodityCommentServiceImpl implements CommodityCommentService {
    @Autowired
    CommodityCommentMapper commodityCommentMapper;
    @Autowired
    OrderMapper orderMapper;
    /**
     * @Author 赵笛
     * @Description TODO 用户新增评论
     * @Date 2023/4/19 20:56
     * @Return com.zhuiYun.project.common.response.CommonResult
     * @param commodityComment
     */
    @Override
    @Transactional
    public CommonResult addComment(CommodityComment commodityComment) {
        //判断必要id是否为空
        if(ObjectUtils.isNotEmpty(commodityComment.getCommodityId())
                &&ObjectUtils.isNotEmpty(commodityComment.getUserId())
                &&ObjectUtils.isNotEmpty(commodityComment.getOrderId())){
            //不为空
            commodityComment.setCreateTime(new Date());
            commodityComment.setUpdateTime(new Date());
            commodityComment.setDelFlag(0);
            int i = commodityCommentMapper.insertUseGeneratedKeys(commodityComment);
            if(i>0){
                //评论成功，修改订单状态
                int j = orderMapper.updateOrderStatusByOrderId(commodityComment.getOrderId(),10);
                if(j>0){
                    //修改成功
                    return CommonResult.ok(EmErrorCode.SAVE_OK);
                }else{
                    //回滚事务
                    int i1 = commodityCommentMapper.deleteByPrimaryKey(commodityComment.getId());
                    if(i1<=0){
                        //强制回滚
                        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    }
                    return CommonResult.error(EmErrorCode.SAVE_ERROR);
                }
            }else {
                //评论未成功
                return CommonResult.error(EmErrorCode.SAVE_ERROR);
            }
        }else{
            //为空
            return CommonResult.error(EmErrorCode.ID_NULL);
        }

    }
}
